package com.leosam.tvbox.mv.proxy;

import java.io.File;
import java.util.concurrent.TimeUnit;

import com.leosam.tvbox.mv.MainVerticle;
import com.leosam.tvbox.mv.data.Vod;
import com.leosam.tvbox.mv.data.VodSeries;
import com.leosam.tvbox.mv.service.MvService;

public class M3u8Proxy {
    private static int CORE_SIZE = Integer.parseInt(System.getProperty("CORE_SIZE", "4"));
    private static M3u8DownloadExecutor threadPool = new M3u8DownloadExecutor(CORE_SIZE, 16, 2, TimeUnit.MINUTES);
    private static String storeBase = MvService.DATA_PATH + MvService.CACHE_PATH;

    public static File getFile(String uri, long timeout) throws InterruptedException {
        File file = threadPool.waitForDownload(uri, storeBase, timeout);
        if (file == null) {
            String[] uriSplit = uri.split("/");
            if (uriSplit.length == 3) {
                String mediaName = uriSplit[0];
                String seriesName = uriSplit[1];
                String segmentName = uriSplit[2];
                String m3u8Uri = getVodSeriesUri(mediaName, seriesName);
                if (m3u8Uri != null) {
                    // 开始index.m3u8的下载
                    String uri0 = mediaName + "/" + seriesName;
                    DownloadRunnable runnable = new M3u8Media(m3u8Uri, uri0).download(threadPool,
                            storeBase + "/" + uri0);
                    File m3u8File = threadPool.waitForDownload(runnable, timeout);
                    if (m3u8File != null && M3u8Media.NAME_INDEX_M3U8.equals(segmentName)) {
                        // 如果请求的就是index.m3u8的下载,则直接返回
                        File noAdsFile = new File(m3u8File.getParent(), M3u8Media.NAME_INDEX_M3U8_NOADS);
                        if (noAdsFile.exists()) {
                            return noAdsFile;
                        } else {
                            return m3u8File;
                        }
                    } else if (m3u8File != null) {
                        // 开始index.m3u8下载后,重试ts文件的下载
                        return threadPool.waitForDownload(uri, storeBase, timeout);
                    }
                }
            }
        } else if (file.getName().equals(M3u8Media.NAME_INDEX_M3U8)) {
            File noAdsFile = new File(file.getParent(), M3u8Media.NAME_INDEX_M3U8_NOADS);
            if (noAdsFile.exists()) {
                return noAdsFile;
            }
        }
        return file;
    }

    /**
     * 触发影片下载
     * 
     * @param mediaName  媒体名
     * @param seriesName 集名
     */
    public static void download(String mediaName, String seriesName) {
        String m3u8Uri = getVodSeriesUri(mediaName, seriesName);
        if (m3u8Uri != null) {
            String uri0 = mediaName + "/" + seriesName;
            DownloadRunnable runnable = new M3u8Media(m3u8Uri, uri0).download(threadPool,
                    storeBase + "/" + uri0);
            threadPool.execute(runnable);
        }
    }

    /**
     * 查找影片的原始uri
     * 
     * @param mediaName  媒体名
     * @param seriesName 集名
     * @return 返回影片的原始url
     */
    private static String getVodSeriesUri(String mediaName, String seriesName) {
        String m3u8Uri = null;
        Vod vod = MainVerticle.mvService.findVod(mediaName);
        if (vod != null && vod.getVodSeries() != null) {
            for (VodSeries series : vod.getVodSeries()) {
                if (seriesName.equals(series.getSeriesName())) {
                    m3u8Uri = series.getOriginUrl();
                    break;
                }
            }
        }
        if (m3u8Uri != null) {
            m3u8Uri = m3u8Uri.trim();
        }
        return m3u8Uri;
    }

}
